//
//  OrdersViewController.swift
//  ERecycle-Shop
//
//  Created by abb on 16/8/10.
//  Copyright © 2016年 abb. All rights reserved.
//

import UIKit
import SVProgressHUD
let OrdersTableViewCellReuseIdentifier = "OrdersTableViewCellReuseIdentifier"
class OrdersViewController: AbbBaseViewController {
    
    var orders:[Order]? {
        didSet {
            tableView.reloadData()
        }
    }
    override var userTransparentNavigationBar: Bool {
        return true
    }

    override func viewDidLoad() {
        super.viewDidLoad()
         self.automaticallyAdjustsScrollViewInsets  = false
        setupUI()
        loadOrders()
        // Do any additional setup after loading the view.
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        merchantinfoView.user = User.loadUser()
    }
    
    func clickSetting() {
        let vc = SettingViewController()
        navigationController?.pushViewController(vc, animated: true)
    }
    
    private func loadOrders() {
        if let id = User.loadUser()?.id {
            SVProgressHUD.showWithStatus("请稍等")
            Order.loadOrderList(id) { (models, error) in
                if error != nil {
                    SVProgressHUD.showErrorWithStatus(error)
                    return
                }
                if models != nil {
                    SVProgressHUD.dismiss()
                    self.orders = models
                }
            }
        }
    }

    /**
     加载UI
     */
    private func setupUI() {
        merchantinfoView.frame = CGRect(x: 0, y: 0, width: UIScreen.mainScreen().bounds.width, height: UIScreen.mainScreen().bounds.height / 3)
        tableView.tableHeaderView = UIView(frame: merchantinfoView.frame)
        view.addSubview(tableView)
        view.addSubview(merchantinfoView)
        tableView.snp_makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(self.view)
        }
        
        // 右边item
        let rightItem = UIBarButtonItem(image: UIImage(named: "setting"), style: .Plain, target: self, action: #selector(clickSetting))
        navigationItem.rightBarButtonItem = rightItem
        
    }
    private lazy var tableView:UITableView = {
        let tb = UITableView(frame: CGRectZero, style: UITableViewStyle.Plain)
        tb.delegate = self
        tb.dataSource = self
        tb.registerClass(OrdersTableViewCell.self, forCellReuseIdentifier: OrdersTableViewCellReuseIdentifier)
        return tb
    }()
    private lazy var merchantinfoView:MerchantInfoView = MerchantInfoView()
}

extension OrdersViewController:UITableViewDelegate,UITableViewDataSource {
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return orders?.count ?? 0
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(OrdersTableViewCellReuseIdentifier, forIndexPath: indexPath) as! OrdersTableViewCell
        cell.order = orders![indexPath.row]
        return cell
    }
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 128
    }
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        let offsetY = scrollView.contentOffset.y
        if offsetY < 0 {
            let totalHeight = UIScreen.mainScreen().bounds.height / 3 - offsetY
            merchantinfoView.frame.size.height = totalHeight
        }
    }
}
